/**
 * Created by Jacky.Gao on 2017-02-07.
 */
import {
    alert,
    confirm
} from '../MsgBox.js';
import {
    setDirty,
    getUrlParam,
    initTable
} from '../Utils.js';

export default class ImportDialog {
    constructor(context, reportTable) {
        this.context = context;
        this.reportTable = reportTable;
        this.dialog = $(`<div class="modal fade data-report-modal" role="dialog" aria-hidden="true" style="z-index: 10000">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                            <i class="report-icon report-icon-huaban16fuben4"/>
                        </button>
                        <h4 class="modal-title">
                            ${window.i18n.dialog.import.title}
                        </h4>
                    </div>
                    <div class="modal-body"></div>
                    <div class="modal-footer">
                    </div>
                </div>
            </div>
        </div>`);
        const body = this.dialog.find('.modal-body'),
            footer = this.dialog.find(".modal-footer");
        this.initBody(body, footer);
    }

    initBody(body, footer) {
        const token = getUrlParam('token')
        const url = window._server + "/import?token=" + token;
        const form = $(`<form enctype="multipart/form-data" action="${url}" method="post" target="_import_excel_frame"></form>`);
        body.append(form);
        form.append(`<div style="margin-bottom: 10px;line-height: 2;color: #929191;">${window.i18n.dialog.import.desc}</div>`);
        const fileGroup = $(`<div class="form-group"><label>${window.i18n.dialog.import.file}</label></div>`);
        form.append(fileGroup);
        // 命名和后端不一样，修改为file
        //const file = $(`<input name="_excel_file" class="form-control" type="file">`);
        const file = $(`<input name="file" class="form-control" type="file">`);
        fileGroup.append(file);
        const buttonGroup = $(`<div class="form-group"></div>`);
        const submit = $(`<button type="button" class="el-button el-button--primary el-button--small">${window.i18n.dialog.import.upload}</button>`);
        buttonGroup.append(submit);
        form.append(buttonGroup);

        let self = this;
        const fileId = getUrlParam('id');
        submit.on('click', function () {
            let formData = new FormData(form[0]);
            $.ajax({
                type: 'post',
                url: url,
                data: formData,
                processData: false,
                contentType: false,
                success: function (res) {
                    let _this = self.reportTable;
                    // console.log(res, _this);
                    if (res && res.code === 200) {
                        const reportDef = res.data
                        initTable(reportDef, {}, !!fileId)
                        // 编辑状态下额外处理
                        if (fileId && res.baseInfo) {
                            _this.baseInfo = res.baseInfo
                        }

                        _this.reportDef = reportDef

                        _this._buildReportData(reportDef);

                        /*if (callback) {
                            callback.call(_this, reportDef);
                        }*/
                        _this.hot.render();

                        if (reportDef.paper.bgImage) {
                            $('.ht_master').css('background', `url(${reportDef.paper.bgImage}) 50px 26px no-repeat`);
                        } else {
                            $('.ht_master').css('background', 'transparent');
                        }
                        self.dialog.modal('hide');
                    } else {
                        alert(`${res.msg}`);
                    }
                }
            })
        });

        // 原来使用iframe获取form表单提交结果
        /*const iframe = $(`<iframe height="0" width="0" src="" name="_import_excel_frame"></iframe>`);
        body.append(iframe);
        iframe.on('load', function(e) {
          const text = iframe.contents().find("body").text();
          if (!text || text === "") {
            return;
          }
          const json = JSON.parse(text);
          const result = json.result;
          console.log(text);
          if (result) {
            const url = window._server + "/designer";
            window.open(url, "_self");
          } else {
            const errorInfo = json.errorInfo;
            if (errorInfo) {
              alert(`${window.i18n.dialog.import.fail}：` + errorInfo);
            } else {
              alert(`${window.i18n.dialog.import.fail}`);
            }
          }
        });*/
    }

    show() {
        this.dialog.modal('show');
    }
}
